package com.iwop.moto.controller;

import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.FVerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/9/6.
 */
@Controller
public class FVerController  extends BaseController {

    @Autowired
    FVerService fVerService;

    /**
     * 参数：name 版本名称
     * 参数：content 更新内容
     * 参数：types 型号
     * 参数：versions 版本号
     */
    @RequestMapping("/api/ver/add")
    @ResponseBody
    public ResultModel fVerAdd() {
        try {
            SuperRecordModel SRModel = this.getSRModel();
            SuperRecordModel superRecordModel=fVerService.isExist(SRModel);
            if(superRecordModel!=null&&superRecordModel.size()>0){
                return getErr(ErrCode.ERROR_VER);
            }
            SRModel.put("sid", UUID.randomUUID().toString());
            SRModel.put("status_flag", "0");
            SRModel.put("applydate", new Date());
            fVerService.insertFVer(SRModel);
            return getSucceed(SRModel, ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 参数：name 版本名称
     * 参数：content 更新内容
     * 参数：types 型号
     * 参数：sid 主键
     * 参数：versions 版本号
     */
    @RequestMapping("/api/ver/edit")
    @ResponseBody
    public ResultModel fVerEdit() {
        try {
            SuperRecordModel SRModel = this.getSRModel();
            SuperRecordModel superRecordModel=fVerService.getFVerByRepeat(SRModel);
            SRModel.put("applydate", new Date());
            fVerService.upFVer(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }


    @RequestMapping("/api/ver/get")
    @ResponseBody
    public ResultModel fVerGet() {
        try {
            SuperRecordModel SRModel = this.getSRModel();
            List<SuperRecordModel> fVer = fVerService.getFVer(SRModel);
            return getSucceed(fVer, ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 参数：type类型 ios，android
     */
    @RequestMapping("/api/ver/detail")
    @ResponseBody
    public ResultModel fVerDetail() {
        try {
            SuperRecordModel SRModel = this.getSRModel();
            SuperRecordModel fVer = fVerService.getFVerDetail(SRModel);
            return getSucceed(fVer, ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    @RequestMapping("/api/ver/get/detail")
    @ResponseBody
    public ResultModel getFVer() {
        try {
            SuperRecordModel SRModel = this.getSRModel();
            SuperRecordModel superRecordModel=fVerService.findFverBySid(SRModel);
            return getSucceed(superRecordModel,ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    @RequestMapping("/api/ver/isupdate")
    @ResponseBody
    public ResultModel fVerIsupdate() {
        return getSucceed("no");
    }

    @RequestMapping("/api/ver/delete")
    @ResponseBody
    public ResultModel fVerDelete() {
         try {
             SuperRecordModel superRecordModel=this.getSRModel();
             int result=fVerService.deleteFVer(superRecordModel);
             if(result==0){
                 return getErr(ErrCode.ERROR_VER_DELETE);
             }else{
                 return getSucceed(ErrCode.SUCCEED);
             }
         }catch (Exception e){
             e.printStackTrace();
             return getErr(ErrCode.ERROR_SYSTEM);
         }
    }

    @RequestMapping("/api/ver/h5")
    @ResponseBody
    public void getFVerInfo(HttpServletResponse response) throws IOException {
        SuperRecordModel srModel = this.getSRModel();
        SuperRecordModel superRecordModel=fVerService.findFVerByType(srModel);
        response.sendRedirect(superRecordModel.getString("uplaod_path"));
    }
}

